/*
Pic Resizer : an easy to use program to resize your pictures
Copyright (C) 2009  Kévin Gomez <geek63@gmail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ProfilsManagerDial.h"

ProfilsManagerDial::ProfilsManagerDial(ProfilesManager *p_manager, QWidget *parent) : QDialog(parent)
{
    setModal(true);
    setWindowTitle(tr("Gestion des profils"));

    //gestionnaire de profiles
    manager = p_manager;

    //création des layout
    QVBoxLayout *global_layout = new QVBoxLayout(this);
    QHBoxLayout *content_layout = new QHBoxLayout();
    QVBoxLayout *side_layout = new QVBoxLayout();
    QHBoxLayout *buttons_layout = new QHBoxLayout();

    //création des widget
    createTable();

    add_profil_w = new QPushButton(tr("Ajouter"), this);
    del_profil_w = new QPushButton(tr("Enlever"), this);
    edit_profil_w = new QPushButton(tr("Modifier"), this);

    close_w = new QPushButton(tr("Fermer"), this);

    //on place les widget dans les layouts
    side_layout->addWidget(add_profil_w);
    side_layout->addWidget(edit_profil_w);
    side_layout->addWidget(del_profil_w);

    content_layout->addWidget(profils_table);
    content_layout->addLayout(side_layout);

    buttons_layout->addWidget(close_w);

    global_layout->addLayout(content_layout);
    global_layout->addLayout(buttons_layout);

    setLayout(global_layout);

    createActions();
}

void ProfilsManagerDial::createActions()
{
    connect(close_w, SIGNAL(clicked()), this, SLOT(reject()));

    connect(add_profil_w, SIGNAL(clicked()), this, SLOT(newProfilDial()));
    connect(edit_profil_w, SIGNAL(clicked()), this, SLOT(editProfilDial()));
    connect(del_profil_w, SIGNAL(clicked()), this, SLOT(delProfilDial()));
}

void ProfilsManagerDial::createTable(bool fill)
{
    QStringList headers = QStringList();
    headers << "Nom" << "Largeur" << "Hauteur" << "Conserver les proportions";

    profils_table = new TableWidget(0, 4, headers, this);
    profils_table->setSelectionMode(QAbstractItemView::SingleSelection);
    profils_table->setSelectionBehavior(QAbstractItemView::SelectRows);

    if(fill)
    {
        fillTable();
    }
}

void ProfilsManagerDial::fillTable()
{
    profils_table->clearContents();
    profils_table->setRowCount(0);

    QList<Profil> list = manager->get();

    for (int i = 0; i < list.size(); ++i)
    {
        profils_table->insertRow(i);

        profils_table->addItem(i, 0, list.at(i).name);
        profils_table->addItem(i, 1, QString::number(list.at(i).width));
        profils_table->addItem(i, 2, QString::number(list.at(i).height));
        profils_table->addItem(i, 3, QString((list.at(i).save_proportions) ?
                                              "Oui" : "Non"));
    }
}

void ProfilsManagerDial::newProfilDial()
{
    ProfilDial *dial = new ProfilDial(manager, this);

    connect(dial, SIGNAL(profilAdded(Profil)), this, SLOT(fillTable()));
    connect(dial, SIGNAL(profilAdded(Profil)), this, SIGNAL(profilsModified()));

    dial->run();
}

void ProfilsManagerDial::editProfilDial()
{
    Profil profil = manager->get(profils_table->item(profils_table->currentRow(), 0)->text());

    ProfilDial *dial = new ProfilDial(manager, &profil, this);

    connect(dial, SIGNAL(profilAdded(Profil)), this, SLOT(fillTable()));
    connect(dial, SIGNAL(profilAdded(Profil)), this, SIGNAL(profilsModified()));

    dial->run();
}

void ProfilsManagerDial::delProfilDial()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, tr("Confirmation "),
                                  tr("Confirmez-vous la suppression de ce profil ?"),
                                  QMessageBox::Yes | QMessageBox::Cancel);

    if(reply == QMessageBox::Yes)
    {
        manager->remove(profils_table->item(profils_table->currentRow(), 0)->text());
        profils_table->removeRow(profils_table->currentRow());

        emit profilsModified();
    }
}

void ProfilsManagerDial::run()
{
    show();
    exec();
}
